package com.twang.stock.service.impl;


import com.twang.stock.entity.TodayQinXu;
import com.twang.stock.entity.TodayTradeAmount;
import com.twang.stock.entity.ZhangTingBan;
import com.twang.stock.entity.ex.TodayQinXuEx;
import com.twang.stock.entity.ex.ZhangTingBanEx;
import com.twang.stock.mapper.TodayQinXuMapper;
import com.twang.stock.mapper.TodayTradeAmountMapper;
import com.twang.stock.service.TodayQinXuService;
import com.twang.stock.service.ZhangTingBanService;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TodayQinXuServiceImpl  implements TodayQinXuService {

    @Resource
    private TodayQinXuMapper todayQinXuMapper;
    public static String TYPE_SHORT_DESC="DESC";
    public static String TYPE_SHORT_ASC="ASC";


    @Resource
    private TodayTradeAmountMapper todayTradeAmountMapper;

    @Autowired
    private ZhangTingBanService zhangTingBanService;

    @Override
    public List<TodayQinXu> queryList(TodayQinXu param,String type) throws Exception {
        Example example = new Example(TodayQinXu.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param.getDate())) {
            criteria.andEqualTo("date", param.getDate());
        }
        if (StringUtils.isNotBlank(param.getMinutes())) {
            criteria.andEqualTo("minutes", param.getMinutes());
        }
        if (TYPE_SHORT_DESC.equals(type)) {
            example.orderBy("id").desc();
        }
        if (TYPE_SHORT_ASC.equals(type)) {
            example.orderBy("id").asc();
        }
        List<TodayQinXu> result = todayQinXuMapper.selectByExample(example);
        //周末节假日没有数据，取最近工作日的日期
        if (CollectionUtils.isEmpty(result)) {
            Example example1 = new Example(TodayQinXu.class);
            Example.Criteria criteria1 = example.createCriteria();
            example1.orderBy("id").desc();
            param.setDate(todayQinXuMapper.selectByExample(example1).get(0).getDate());
          return  queryList(param, type);
        }
        return result;
    }

    @Override
    public List<TodayQinXu> queryByDate(String date) throws Exception {
        Example example = new Example(TodayQinXu.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(date)) {
            criteria.andEqualTo("date", date);
        } else {
            throw new Exception("请输入日期");
        }
        List<TodayQinXu> list= todayQinXuMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<TodayTradeAmount> queryTrade50AmountList(TodayTradeAmount param, String type) throws Exception {
        Example example = new Example(TodayTradeAmount.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param.getDate())) {
            criteria.andEqualTo("date", param.getDate());
        }
        if (StringUtils.isNotBlank(param.getMinutes())) {
            criteria.andEqualTo("minutes", param.getMinutes());
        }
        if (TYPE_SHORT_DESC.equals(type)) {
            example.orderBy("minutes").desc();
        }
        if (TYPE_SHORT_ASC.equals(type)) {
            example.orderBy("minutes").asc();
        }
        List<TodayTradeAmount> result = todayTradeAmountMapper.selectByExample(example);
        //周末节假日没有数据，取最近工作日的日期
        if (CollectionUtils.isEmpty(result)) {
            Example example1 = new Example(TodayTradeAmount.class);
            Example.Criteria criteria1 = example.createCriteria();
            example1.orderBy("id").desc();
            param.setDate(todayTradeAmountMapper.selectByExample(example1).get(0).getDate());
            return  queryTrade50AmountList(param, type);
        }
        return result;
    }

    @Override
    public TodayQinXu queryByDayLastOne(String date) {
        return todayQinXuMapper.queryByDayLastOne(date);
    }

    /**
     * 涨停板
     * @param modelAndView
     * @throws Exception
     */
    public void zhangTingBanQuery(ModelAndView modelAndView) throws Exception {
        TodayQinXu param = new TodayQinXu();
        param.setDate(HolidayCopy.getRealToday());
        List<TodayQinXu> result = queryList(param, TodayQinXuServiceImpl.TYPE_SHORT_ASC);
        //当日行情信息
        TodayQinXu todayQinXu = result.get(result.size() - 1);
        TodayQinXuEx todayQinXuEx = new TodayQinXuEx();
        BeanUtils.copyProperties(todayQinXu, todayQinXuEx);
        todayQinXuEx.setShTradeSum(todayQinXuEx.getShTradeSum().divide(new BigDecimal(100000000), 2, BigDecimal.ROUND_HALF_UP));
        todayQinXuEx.setSzTradeSum(todayQinXuEx.getSzTradeSum().divide(new BigDecimal(100000000), 2, BigDecimal.ROUND_HALF_UP));
        todayQinXuEx.setTime(HolidayCopy.getToday年月日分时秒(todayQinXuEx.getDate(), todayQinXuEx.getMinutes()));
        /**
         * 查詢漲停板：今日
         */
        Map<String,List<ZhangTingBan>> ztbMap=new HashMap<>();
        ZhangTingBan zhangTingBanParam=new ZhangTingBan();
        zhangTingBanParam.setDate(param.getDate());
        ZhangTingBanEx zhangTingBanEx= zhangTingBanService.queryZhangTingBan(zhangTingBanParam,ztbMap);
        todayQinXuEx.setFirst(zhangTingBanEx.getFirst());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.FIRST_一板,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.FIRST_一板));
        todayQinXuEx.setSecond(zhangTingBanEx.getSecond());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.SECOND_二板,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.SECOND_二板));
        todayQinXuEx.setThird(zhangTingBanEx.getThird());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.THIRD_三板,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.THIRD_三板));
        todayQinXuEx.setFouth(zhangTingBanEx.getFouth());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.FOURTH_四板,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.FOURTH_四板));
        todayQinXuEx.setFive(zhangTingBanEx.getFive());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.FIVE_五板,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.FIVE_五板));
        todayQinXuEx.setMore(zhangTingBanEx.getMore());
        modelAndView.addObject(ZhangTingBanServiceImpl.ZhangTingBanCommon.MORE_更高,ztbMap.get(ZhangTingBanServiceImpl.ZhangTingBanCommon.MORE_更高));

        /**
         * 返回
         */
        modelAndView.addObject("todayQinXuNow", todayQinXuEx);
//        modelAndView.addObject("num", num);
    }

}
